package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.dto.yingshi.YingShiMonitorQueryDto;
import com.csun.cmny.provider.model.dto.yingshi.YingshiMonitorDto;
import com.csun.cmny.provider.model.dto.yingshi.YingshiMonitorEditDto;
import com.csun.cmny.provider.model.vo.GroupVo;
import com.csun.cmny.provider.model.vo.yingshi.YingShiMonitorVo;
import com.csun.cmny.provider.model.vo.yingshi.YingShiPlayerVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcYingShiMonitorService;
import com.csun.cmny.provider.service.UacRpcService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021/11/8 13:35
 * @Description: 萤石摄像头监控控制器
 */
@RestController
@RequestMapping(value = "/yingShiMonitor", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcYingShiMonitorController extends BaseController {

    @Resource
    private DmcYingShiMonitorService dmcYingShiMonitorService;
    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private UacRpcService uacRpcService;

    @RequestMapping(value = "/queryPlayList", method = RequestMethod.POST)
    public Wrapper queryPlayList() {

        List<YingShiPlayerVo> yingShiPlayerVoList = Lists.newArrayList();

        Long groupId = getLoginAuthDto().getGroupId();

        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(groupId);
        List<Long> groupIdList = Lists.newArrayList();
        if (PublicUtil.isNotEmpty(groupVoList)) {
            for (GroupVo vo : groupVoList) {
                groupIdList.add(vo.getId());
            }

            yingShiPlayerVoList = dmcYingShiMonitorService.queryPlayList(groupIdList);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                yingShiPlayerVoList);
    }

    /**
     * 查询萤石摄像头视频监控列表 - 分页
     *
     * @param yingShiMonitorQueryDto the ying shi monitor query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody YingShiMonitorQueryDto yingShiMonitorQueryDto) {

        List<YingShiMonitorVo> yingShiMonitorVoList = Lists.newArrayList();

        Long groupId = getLoginAuthDto().getGroupId();

        List<GroupVo> groupVoList = uacRpcService.queryGroupListByGroupId(groupId);
        List<Long> groupIdList = Lists.newArrayList();
        if (PublicUtil.isNotEmpty(groupVoList)) {
            for (GroupVo vo : groupVoList) {
                groupIdList.add(vo.getId());
            }
            yingShiMonitorQueryDto.setGroupIdList(groupIdList);

            yingShiMonitorVoList = dmcYingShiMonitorService.queryListWithPage(yingShiMonitorQueryDto);
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(yingShiMonitorVoList));
    }

    /**
     * 添加萤石摄像头通道监控
     *
     * @param channelId  the ying shi channel id
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/addSelect", method = RequestMethod.POST)
    public Wrapper addSelect(Long channelId) {

        Integer result = dmcYingShiMonitorService.addSelect(channelId, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 添加萤石摄像头通道监控
     *
     * @param yingshiMonitorDto  the ying shi monitor dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper save(@RequestBody YingshiMonitorDto yingshiMonitorDto) {

        Integer result = dmcYingShiMonitorService.save(yingshiMonitorDto.getChannelIdList(), getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 修改萤石摄像头通道监控信息(序号)
     *
     * @param yingshiMonitorEditDto  the ying shi monitor edit dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper edit(@RequestBody YingshiMonitorEditDto yingshiMonitorEditDto) {

        Integer result = dmcYingShiMonitorService.edit(yingshiMonitorEditDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 根据id删除萤石摄像头通道监控
     *
     * @param id    the id
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = dmcYingShiMonitorService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

}
